import { Component, OnInit, Host } from '@angular/core';
import { News } from '../../model/news';
import { NewsService } from '../../service/news.service';
import { Router, ActivatedRoute } from '@angular/router';
import { ConfigService } from '../../service/config.service';
@Component({
  selector: 'app-newslist',
  templateUrl: './newslist.component.html',
  styleUrls: ['./newslist.component.css']
})
export class NewslistComponent implements OnInit {
nl:Array<News>;
HOST:string;
  constructor(
    private newsservice:NewsService,
    private router:Router,
    private routerinfo:ActivatedRoute,
    private conifg:ConfigService
    ) { 
       this.HOST=conifg.host+"/public/"

    }

  ngOnInit() {
    this.loadNewsList();

  }
  private loadNewsList(){
    this.nl=new Array();
  
    this.newsservice.getNewsList()
    .then((data:any)=>{
  this.nl=data;


    })
  }
  addNews(){
    this.router.navigate(['editnews','add']);

   }

   editNews(id:string){

     this.router.navigate(['editnews',id]);

   }
   //删除单个对象
  deleteNews(id:string){
    this.newsservice.deleteNews(id)
    .then((flag:boolean)=>{
      if(flag==true){
        this.loadNewsList();
      }
      else{
        alert("操作异常，请刷新后重试");
      }
    })
  }
  
  toDetail(id:string){

    this.router.navigate(['detail',id]);

    }

    selectedFile: string;
 
  onFileChanged(event) {
    this.selectedFile = event.target.files[0];
   
  }

  uploadNewsCover(newsid:string){


    this.newsservice.uploadNewsCoverPic(newsid,this.selectedFile)
    .then((data:any)=>{
 
 
    if(data.filename){
      this.loadNewsList();
    }
    //console.dir(data);
 
 
    })
 
 
   }


  
}
